package etnorservice.etnor_service.service;

import etnorservice.etnor_service.domain.po.DocumentSpace;
import etnorservice.etnor_service.mapper.ArticleMapper;
import etnorservice.etnor_service.mapper.WorkspaceTreeMapper;
import etnorservice.etnor_service.request.SpaceCreateRequest;
import etnorservice.etnor_service.utils.UniqueIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/10/16/11:56
 **/
@Service
public class DocumentSpaceService {
    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    WorkspaceTreeMapper workspaceTreeMapper;


    /**
     * 根据给定的 ID 查找文档空间，并根据 isTopLevel 和 parentId 字段进行递归处理，返回符合条件的空间集合。
     *
     * @param id 文档空间的 ID
     * @return 符合条件的空间集合
     */
    public List<DocumentSpace> findDocumentSpaces(String id) {
       Set<DocumentSpace> resultSet = new HashSet<>();
       DocumentSpace documentSpace = articleMapper.findDocumentSpaceById(id);

       if (documentSpace != null) {
           if (documentSpace.getIsTopLevel()) {
               resultSet.add(documentSpace);
           } else {
               String parentId = documentSpace.getParentId();
               if (parentId != null) {
                   List<DocumentSpace> parentSpaces = findDocumentSpaces(parentId);
                   if (!parentSpaces.isEmpty()) {
                       resultSet.addAll(parentSpaces);
                   }
               }
               resultSet.add(documentSpace);
           }
           // 检查是否有同级节点
//           if (workspaceTreeMapper.findAllBrother(id).size()>1) {
               List<DocumentSpace> brotherSpaces = workspaceTreeMapper.findAllBrother(documentSpace.getParentId());
               if (!brotherSpaces.isEmpty()) {
                   resultSet.addAll(brotherSpaces);
               }
           //}
       }

       return new ArrayList<>(resultSet);
   }

    /**
     * 根据用户账号获取空间号
     */
    public List<String> getSpaceIdByAccount(String account){
        return workspaceTreeMapper.findUserSpaceIdByAccount(account);
    }

    public DocumentSpace findSpaceNameBySpaceId(String id){
        return workspaceTreeMapper.findSpaceNameById(id);
    }
    public List<DocumentSpace> findHaveChildrenNode(String spaceId){
        return workspaceTreeMapper.findHaveChildrenNode(spaceId);
    }
    public void deleteSpaceById(String spaceId,String account){
        //如果没有兄弟节点，需要对这个spaceId做映射调整

        List<DocumentSpace> allBrother = workspaceTreeMapper.findAllBrother(spaceId);

        //没有同级节点
        if(allBrother.size()==0){
            DocumentSpace spaceNameById = workspaceTreeMapper.findSpaceNameById(spaceId);
            //开发者注：此处表示当前删除的节点没有同级节点，需要将映射当前节点的记录变为父节点
            workspaceTreeMapper.setNewSpaceHook(spaceNameById.getParentId(),account);
        }
        workspaceTreeMapper.updateFlexTable(spaceId);
        workspaceTreeMapper.deleteSpaceById(spaceId);
    }
    public void createDocumentSpace(SpaceCreateRequest spaceCreateRequest) throws SQLException {
        spaceCreateRequest.setSpaceId(UniqueIdGenerator.generateUUID());
        spaceCreateRequest.setIsTopLevel(0);
        spaceCreateRequest.setCreatedAt(new Date());
        spaceCreateRequest.setSpaceStatus("active");
        spaceCreateRequest.setIsEdit(1);
        spaceCreateRequest.setIsDelete(1);

        workspaceTreeMapper.insertSpace(spaceCreateRequest);
        workspaceTreeMapper.createFlex(spaceCreateRequest.getSpaceId(),spaceCreateRequest.getOwnerId());
         }

}

